#view.py
from PySide6.QtWidgets import QMainWindow, QMenuBar, QMenu, QVBoxLayout, QHBoxLayout, QWidget, QLabel, QPushButton, QGroupBox, QGridLayout, QFileDialog, QMessageBox
from PySide6.QtGui import QIcon, QAction
from PySide6.QtCore import Qt, QSize

class MainWindow(QMainWindow):
    def __init__(self, controller):
        super().__init__()
        self.controller = controller
        self.initUI()

    def initUI(self):
        self.setWindowTitle("MVC Pattern Example")
        self.setGeometry(100, 100, 300, 200)

        # 创建菜单栏
        menubar = self.menuBar()

        # 文件菜单
        file_menu = menubar.addMenu("文件")
        exit_action = QAction("退出", self)
        exit_action.triggered.connect(self.close)
        file_menu.addAction(exit_action)

        # 帮助菜单
        help_menu = menubar.addMenu("帮助")
        about_action = QAction("关于", self)
        theme_action = QAction("主题", self)
        help_menu.addAction(about_action)
        help_menu.addAction(theme_action)

        # 主布局
        main_layout = QVBoxLayout()

        # AD Group Box
        ad_group_box = QGroupBox("AD")
        ad_layout = QGridLayout()
        ad_icon_label = QLabel()
        ad_icon_label.setAlignment(Qt.AlignCenter)
        ad_icon_label.setFixedSize(50, 50)
        ad_layout.addWidget(ad_icon_label, 0, 0)
        ad_text_label = QLabel("运行")
        ad_text_label.setAlignment(Qt.AlignCenter)
        ad_layout.addWidget(ad_text_label, 1, 0)
        ad_group_box.setLayout(ad_layout)
        ad_group_box.mousePressEvent = self.controller.open_sub_window

        # 功能 Group Box
        func_group_box = QGroupBox("功能")
        func_layout = QGridLayout()
        func_group_box.setLayout(func_layout)

        # 添加到主布局
        main_layout.addWidget(ad_group_box)
        main_layout.addWidget(func_group_box)

        container = QWidget()
        container.setLayout(main_layout)
        self.setCentralWidget(container)

class SubWindow(QWidget):
    def __init__(self, controller):
        super().__init__()
        self.controller = controller
        self.initUI()

    def initUI(self):
        self.setWindowTitle("子窗口")
        self.setGeometry(300, 300, 300, 200)

        layout = QVBoxLayout()

        self.time_label = QLabel("当前时间：")
        layout.addWidget(self.time_label)

        self.pause_button = QPushButton("暂停")
        self.pause_button.clicked.connect(self.controller.toggle_pause)
        layout.addWidget(self.pause_button)

        self.exit_button = QPushButton("退出")
        self.exit_button.clicked.connect(self.close)
        layout.addWidget(self.exit_button)

        self.setLayout(layout)

    def update_time(self, time_str):
        self.time_label.setText(f"当前时间：{time_str}")